package com.qys.livesMall.navy.init;

import cn.hutool.core.util.RandomUtil;
import com.qys.livesMall.navy.common.Constant;
import com.qys.livesMall.navy.model.entity.NavyTask;
import com.qys.livesMall.navy.model.entity.NavyTaskDetail;
import com.qys.livesMall.navy.service.NavyShopAutoOrderServer;
import com.qys.livesMall.navy.service.NavyTaskDetailService;
import com.qys.livesMall.navy.service.NavyTaskService;
import com.qys.livesMall.navy.task.TaskInter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @menu TaskInterInit
 * @date 2023/07/20 16:51
 */
@Slf4j
@Component
public class TaskInterInit implements CommandLineRunner {

    @Autowired
    private NavyTaskService navyTaskService;

    @Autowired
    private NavyTaskDetailService navyTaskDetailService;

    @Autowired
    private NavyShopAutoOrderServer navyShopAutoOrderServer;

    @Autowired
    private TaskInter taskInter;

    @Override
    public void run(String... args) throws Exception {
        new Thread(()-> {
            //获取所有任务
            log.error("初始化水军任务....");
            List<NavyTask> taskList = navyTaskService.listNoEnd();
            if (CollectionUtils.isEmpty(taskList)) {
                taskList = new ArrayList<>();
            }
            for (NavyTask task : taskList) {
                if (Objects.equals(task.getTaskType(), Constant.TaskType.SHOP_ORDER)) {
                    //获取所有明细
                    List<NavyTaskDetail> details = navyTaskDetailService.getAllNoEnd(task.getId());
                    if (CollectionUtils.isEmpty(details)) {
                        navyTaskService.watchTaskEnd(task.getId());
                        continue;
                    } else {
                        details.stream().forEach(detail -> {
                            taskInter.addShopOrderTask(RandomUtil.randomInt(600, 5400), detail.getId());
                        });
                        continue;
                    }
                }
                //获取子任务
                NavyTaskDetail detail = navyTaskDetailService.getNextDetail(task.getId());
                if (detail == null) {
                    if (Objects.equals(task.getTaskType(), Constant.TaskType.lIVES_ORDER) && Constant.TaskStatus.RUNNING.equals(task.getTaskStatus())) {
                        taskInter.addLivesOrderTask(RandomUtil.randomInt(60, 70), task.getId());
                        continue;
                    } else {
                        navyTaskService.watchTaskEnd(task.getId());
                        return;
                    }
                }
                switch (detail.getTaskType()) {
//                Constant.TaskType.SHOP_COLLECT
                    case 1:
                        taskInter.addShopCollectTask(RandomUtil.randomInt(600, 5400), detail.getId());
                        break;
                    case 2:
                        taskInter.addLivesGiveLikeTask(RandomUtil.randomInt(600, 5400), detail.getId());
                        break;
                    case 5:
                        taskInter.addVideoCommentTask(RandomUtil.randomInt(600, 5400), detail.getId());
                        break;
                    case 6:
                        taskInter.addGoodsCommentTask(RandomUtil.randomInt(600, 5400), detail.getId());
                        break;
                    case 7:
                        taskInter.addLivesGiftTask(RandomUtil.randomInt(600, 5400), detail.getId());
                        break;
                    case 8:
                        taskInter.addLivesBulletScreenTask(RandomUtil.randomInt(600, 5400), detail.getId());
                        break;
                    default:
                }
            }
            log.info("初始化水军任务完成");
            log.info("开始初始化打单。。。");
            navyShopAutoOrderServer.init();
            log.info("初始化打单任务完成");
        }).start();
    }
}

